package LeetCode;

public class LC_543_DiameterofBinaryTree {

    public static void main(String[] args) {

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public class Solution {
        int diameter = 0;

        public int diameterOfBinaryTree(TreeNode root) {
            getDepth(root);
            return diameter;
        }

        private int getDepth(TreeNode root) {
            if (root == null) return 0;
            int l = getDepth(root.left);
            int r = getDepth(root.right);
            diameter = Math.max(diameter, l + r);
            return Math.max(l, r) + 1;
        }
    }
}